// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        holdStartTime:0,
        MAX_IMPULSE:500, //最大冲量
        MIN_IMPULSE:100, //最小冲量
        RESTITUTION:0.6,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START, function(event) {
            cc.log("switch TOUCH_START");
            this.holdStartTime = new Date().getTime();
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_END, function(event) {
            cc.log("switch TOUCH_END");
            if (this.ball != null) {
                let holdTime = new Date().getTime() - this.holdStartTime;
                let impulse = Math.min(this.MAX_IMPULSE, this.MIN_IMPULSE + this.MAX_IMPULSE*holdTime/5000); //5秒蓄力最大
                let ballBody = this.ball.getComponent(cc.RigidBody);
                ballBody.applyLinearImpulse(cc.v2(0, impulse), ballBody.getWorldPosition());
                this.ball.getComponent(cc.PhysicsCircleCollider).restitution = this.RESTITUTION;
                cc.log("impulse="+impulse);
            }
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(event) {
            cc.log("switch TOUCH_CANCEL");
            this.holdStartTime = 0;
        }, this);
        this.node.on(cc.Node.EventType.MOUSE_DOWN, function(event) {
            cc.log("switch MOUSE_DOWN");
        }, this);
    },

    start () {

    },

 // 只在两个碰撞体开始接触时被调用一次
    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag == 100) {
            cc.log("switch collison ball");
            this.ball = otherCollider;
            this.ball.getComponent(cc.PhysicsCircleCollider).restitution = 0;
        }
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
        cc.log("switch collison ball end");
        if (otherCollider.tag == 100) {
            this.ball = null;
        }
    },

    // update (dt) {},
});
